package com.mc.developmentkit.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import com.qamaster.android.R;
import com.mc.developmentkit.config.MCConstant;
import com.mc.developmentkit.https.HttpUtils;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.TitleManger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by LeBron on 2017/1/3.
 */

public class FeedBackActivity extends Activity{

    private EditText edit;
    private EditText edit1;
    private View fasong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        TitleManger insetance = TitleManger.getInsetance();
        insetance.setContext(this);
        insetance.setName("意见反馈");
        initView();
    }

    private void initView() {

        edit = (EditText)findViewById(R.id.edit);
        edit1 = (EditText)findViewById(R.id.edit1);
        fasong = findViewById(R.id.button1);
        fasong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = edit.getText().toString();
                String s1 = edit1.getText().toString();
                if(s.equals("")){
                    ToastUtil.showToast("请输入内容");
                    return;
                }
                if(s1.equals("")){
                    ToastUtil.showToast("请输入联系方式");
                    return;
                }

                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("user_id", MCUtils.LoginUser().id);
                    jsonObject.put("contact",s1);
                    jsonObject.put("content",s);
                    HttpUtils.POST(handler, MCConstant.getUserUserFeedBackUrlUrl(),jsonObject.toString(),true);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("意见反馈json",(String)msg.obj);
                    try {
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        int status = jsonObject.getInt("status");
                        if(status==1){
                            ToastUtil.showToast("提交成功");
                            finish();
                        }else{
                            ToastUtil.showToast("提交失败");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("意见反馈出错",e.toString());
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };


}
